/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package SaleManagerServer.Util;

import SaleManagerServer.Pojo.LocationPojo;
import SaleManagerServer.Pojo.PositionPOJO;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 * @author Ven Nguyen
 */
public class LocationStringProcessing {
    static String LocationSeparateCharacter = "\r\n";
    static String FieldSeparateChar = ";";

    /**
     * Convert location string received from user to Location pojo
     * @param input: location string format as: Userid;latitude;longtitude;time\r\n <next location>
     * @return list location as Pojo object
     */
    public static List<PositionPOJO> formattedtringToLocation(String input)
    {
        List<PositionPOJO> locations = new ArrayList<PositionPOJO>();
        if(input != null)
        {
            String []locationStrings = input.split(LocationSeparateCharacter);
            for(int i = 0; i < locationStrings.length; i++)
            {
                PositionPOJO location = ProcessALocation(locationStrings[i]);
                if(location != null)
                {
                locations.add(ProcessALocation(locationStrings[i]));
                }
            }
        }
        return locations;
    }

    private static PositionPOJO ProcessALocation(String locationStr) {
        String [] fields = locationStr.split(FieldSeparateChar);
        PositionPOJO location = null;
        if(fields.length > Constants.POS_TIME)
        {
            try{
                Date postiontinDate = new SimpleDateFormat("dd/MM/yyyy HH:mm").parse(fields[Constants.POS_TIME]);
                location = new PositionPOJO(Integer.valueOf(fields[Constants.POS_USER_ID]),
                        Double.valueOf(fields[Constants.POS_LAT]),
                        Double.valueOf(fields[Constants.POS_LANG]), new java.sql.Date( postiontinDate.getTime()));
            } catch (Exception e)
            {
            }
        }

        return location;
    }
}
